package server

import (
	"bc/util"
	"bc/util/library/zstack"
	"fmt"
)

type imbs struct {

}
/**
 添加镜像仓库
 */
func (t *imbs) AddImageStoreBackupStorage (oauth string,zone_uuid string,body string) (string,error) {

	url := util.Config.Zstack.ApiUrl + "v1/backup-storage/image-store"
	header := make(map[string]string)
	header["Content-Type"] = "application/json;charset=UTF-8"
	header["Authorization"] = "OAuth " + oauth
	header["X-Web-Hook"] = util.Config.Zstack.WebHook + "image_server_add_call_back?zone_uuid="+zone_uuid + "&oauth="+oauth

	result,err := util.Curl.Header(header).Body(body).Url(url).POST()
	return result,err

}
/**
挂载服务器到区域
 */
func (t *imbs)AttachBackupStorageToZone(zone_uuid string,oauth string,backupStorageUuid string) (string ,error){

	url := fmt.Sprintf(util.Config.Zstack.ApiUrl + "v1/zones/%s/backup-storage/%s",zone_uuid,backupStorageUuid)
	res , err := util.Curl.Header(map[string]string{
		"Content-Type":"application/json;charset=UTF-8",
		"Authorization":"OAuth " + oauth,
		"X-Web-Hook" :util.Config.Zstack.WebHook + "to_zone_call_back",
	}).Url(url).POST()

	if err != nil{
		return "",err
	}
	return res,nil

}
/**
	删除镜像服务器
 */
 func (t *imbs)DeleteBackupStorage(uuid string,dele_mode string) (string,error){

 	oauth,err := zstack.Oauth()
 	if err != nil{
 		panic(err)
	}
 	url := fmt.Sprintf(util.Config.Zstack.ApiUrl + "v1/backup-storage/%s?deleteMode=%s",uuid,dele_mode)
 	res,err := util.Curl.Header(map[string]string{
		"Content-Type":"application/json;charset=UTF-8",
		"Authorization":"OAuth " + oauth,
		"X-Web-Hook":util.Config.Zstack.WebHook + "del_image_server_call_back?uuid=" + uuid,
	}).Url(url).DELETE()
	return res,err

 }
var ImageBackupStorage = &imbs{}
